import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:ball_lion/entity/record/points_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';
import 'package:ball_lion/widgets/base/wqs_dialog.dart';

class DataLine extends StatelessWidget {
  const DataLine(
      {super.key,
      this.team,
      this.isHeader = false,
      this.qualify,
      this.headType,
      this.index,
      required this.recordType});
  const DataLine.soccer(
      {super.key,
      this.team,
      this.isHeader = false,
      this.qualify,
      this.headType,
      this.index})
      : recordType = 0;
  const DataLine.basketball(
      {super.key,
      this.team,
      this.isHeader = false,
      this.qualify,
      this.headType,
      this.index})
      : recordType = 1;

  final int recordType;
  final RankingTeam? team;
  final RankingQualify? qualify;
  final bool isHeader;
  final String? headType;
  final int? index;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        if (team?.teamId != null) {
          Get.toNamed(
              recordType == 0 ? Routes.zqTeamDetail : Routes.lqTeamDetail,
              arguments: team?.teamId);
        }
      },
      child: Stack(
        alignment: Alignment.topCenter,
        children: [
          Container(
            color: (index ?? 0) >= ((qualify?.beginRank ?? 0) - 1) &&
                    (index ?? 0) <= ((qualify?.endRank ?? -1) - 1)
                ? HexColor(qualify?.color ?? ("#FFFFFF"))
                : null,
            height: isHeader ? null : 50,
            padding: EdgeInsets.only(right: isHeader ? 0 : 16),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                if (isHeader) ...[
                  Expanded(
                    flex: 5,
                    child:
                        QsText(headType ?? "", fontSize: 13, color: Colours.N8)
                            .paddingOnly(left: 8),
                  )
                ] else ...[
                  Expanded(
                    child: Container(
                        width: 36,
                        alignment: Alignment.center,
                        child: QsText("${team?.rank ?? "-"}",
                            fontSize: 13, color: Colours.N8)),
                  ),
                  Flexible(
                    flex: 1,
                    fit: FlexFit.tight,
                    child: Container(
                      alignment: Alignment.centerLeft,
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(1),
                        child: CachedNetworkImage(
                            width: 24,
                            height: 24,
                            placeholder: (context, url) =>
                                Styles.placeholderIcon(),
                            errorWidget: (context, url, error) => Image.asset(
                                Utils.getImgPath(
                                    "${recordType == 0 ? "zq" : "lq"}_placeholder.png")),
                            imageUrl: team?.teamLogo ?? ""),
                      ),
                    ),
                  ),
                  Flexible(
                    flex: 3,
                    fit: FlexFit.tight,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        QsText(
                          team?.teamName ?? "",
                          fontSize: 13,
                          maxLines: 2,
                        ),
                      ],
                    ),
                  )
                ],
                Flexible(flex: 8, child: _data()),
              ],
            ),
          ),
          if (team?.explain.hasValue == true)
            Positioned(
                top: 0,
                bottom: 0,
                right: 8,
                child: GestureDetector(
                  onTap: () => Get.dialog(QsDialog.single("扣分说明",
                      content: team?.explain ?? "", confirmText: "我知道了")),
                  child: Image.asset(
                      width: 12,
                      height: 12,
                      Utils.getImgPath("question_mark.png")),
                )),
          if (qualify != null && index == (qualify?.beginRank ?? 0) - 1)
            Positioned(
                top: 1,
                right: 0,
                child: Container(
                    decoration: BoxDecoration(
                        color: qualify?.tagColor != null &&
                                qualify?.tagColor != "#ffffff"
                            ? HexColor(qualify?.tagColor ?? ("#000000"))
                            : null,
                        borderRadius: const BorderRadius.vertical(
                            bottom: Radius.circular(2))),
                    padding:
                        const EdgeInsets.symmetric(horizontal: 2, vertical: 1),
                    child: QsText(qualify?.name ?? "",
                        fontSize: 10, color: Colours.N1)))
        ],
      ),
    );
  }

  Widget _data() {
    List<double> size =
        recordType == 0 ? [28, 60, 45, 28, 28] : [60, 45, 28, 45];
    List<String> header = recordType == 0
        ? ["赛", "胜/平/负", "进/失", "净", "积分"]
        : ["胜/负", "胜率", "胜差", "近况"];
    List<String>? data =
        recordType == 0 ? team?.dataLineSoccer : team?.dataLineBasketball;
    return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: List.generate(
            size.length,
            (index) => Container(
                width: size[index],
                alignment: Alignment.center,
                child: QsText(isHeader ? header[index] : data?[index] ?? "",
                    fontSize: 13,
                    color: isHeader ? Colours.N8 : Colours.text))));
  }
}
